<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-Button type="info" @click="exit()">退出</el-Button>
    </el-header>
    <el-container>
      <el-aside :width="iscollapse ? '64px' : '200px' ">
       <!-- 折叠按钮 -->
       <div @click="toggleButton()" class="toggle-button">|||</div>
        <!-- 菜单区 -->
        <el-menu
        active-text-color="#409EEF"
          background-color="#333744"
          text-color="#fff"
          unique-opened
          :collapse = "iscollapse"
          :collapse-transition = "false"
          router
          :default-active="menu_item_active"
        >
          <!-- 一级菜单区 -->
          <el-submenu :index="item.id.toString() " v-for="item in Menus" :key="item.id">
            <!-- 一级菜单内容区 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
              <el-menu-item :index=" '/' + Subitem.path" v-for="Subitem in item.children" :key="Subitem.id" @click="active('/' + Subitem.path)">
                <template slot="title">
                  <i class="el-icon-location"></i>
                  <span>{{ Subitem.authName }}</span>
                </template>
              </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      // 保存菜单项
      Menus: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont  icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      iscollapse: false,
      menu_item_active: ''
    }
  },

  //   生命周期函数
  mounted() {
    this.getMenus()
    this.menu_item_active = window.sessionStorage.getItem('active')
  },
  methods: {
    exit() {
      //   删除token值
      window.sessionStorage.removeItem('token')
      //   跳转到/login
      this.$router.push('/login')
    },
    // 获取左侧菜单项
    getMenus() {
      // 获取接口 Menus
      this.$http.get('menus').then(res => {
        this.Menus = res.data.data
      })
    },
    // 菜单栏折叠
    toggleButton() {
      this.iscollapse = !this.iscollapse
    },
    // 菜单栏高亮
    active(active) {
      // 把地址存入sessionStorage里面
      window.sessionStorage.setItem('active', active)
      this.menu_item_active = active
    }
  }
}
</script>
<style lang="less">
.home-container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 0;
    > div {
      display: flex;
      align-items: center;
      span {
        font-size: 20px;
        color: whitesmoke;
        margin-left: 15px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    .toggle-button {
      width: 100%;
      height: 24px;
      text-align: center;
      line-height: 24px;
      letter-spacing: 0.2em;
      cursor: pointer;
      color: whitesmoke;
      background-color: #504b4b;
      margin-bottom: 5px;
    }
    .el-menu {
      border-right: none;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
  .iconfont {
    margin-right: 10px;
  }
}
</style>
